﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace NextCore
{
    public class TexBase
    {
        public int ID = 0;
        public bool Bound = false;
    }
    public class Tex2D : TexBase
    {
        public int W, H;
        public System.Drawing.Bitmap Raw = null;
        public int MagFilter = (int)TextureMagFilter.Nearest;
        public int MinFilter = (int)TextureMinFilter.Nearest;
        public int WrapS = (int)TextureWrapMode.ClampToEdge;
        public int WrapT = (int)TextureWrapMode.ClampToEdge;
        public Tex2D(string path)
        {
            Raw = new System.Drawing.Bitmap(path);
            byte[] pix = new byte[Raw.Width * Raw.Height * 4];
            W = Raw.Width;
            H = Raw.Height;
            int pc = 0;
            for (int y = 0; y < H; y++)
            {
                for (int x = 0; x < W; x++)
                {
                    var p = Raw.GetPixel(x, y);
                    pix[pc++] = p.R;
                    pix[pc++] = p.G;
                    pix[pc++] = p.B;
                    pix[pc++] = p.A;
                }
            }

            ID = GL.GenTexture();
           
            GL.Enable(EnableCap.Texture2D);
            GL.BindTexture(TextureTarget.Texture2D, ID);
            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
            GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ref MagFilter);
            GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ref MinFilter);
            GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, ref WrapS);
            GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, ref WrapT);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, W, H, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pix);

            GL.Disable(EnableCap.Texture2D);
            NextApp.Log.Log(LogType.Load, "Loaded Tex2D ID:" + ID, "Tex2D", this as object, DateTime.Now);
            NextApp.Log.ToConsole();
        }
    }
}
